package handler

import (
	"github.com/gin-gonic/gin"
	"go_shengshu_admin/enum"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/resp"
	"go_shengshu_admin/service"
	"net/http"
)

type OrderHandler struct {
	Srv *service.OrderService
}

func (h *OrderHandler) GetEntity(order model.Order) resp.Order {
	return resp.Order{
		OrderId:     order.OrderId,
		UserId:      order.UserId,
		NickName:    order.NickName,
		Mobile:      order.Mobile,
		TotalPrice:  order.TotalPrice,
		PayStatus:   order.PayStatus,
		PayType:     order.PayType,
		PayTime:     order.PayTime,
		OrderStatus: order.OrderStatus,
		ExtraInfo:   order.ExtraInfo,
		UserAddress: order.UserAddress,
		IsDeleted:   order.IsDeleted,
	}
}

func (h *OrderHandler) OrderInfoHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	order := model.Order{
		OrderId: id,
	}
	result, err := h.Srv.GetOrder(order)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      result,
		Total:     0,
		TotalPage: 0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *OrderHandler) OrderListHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var q query.ListQuery
	if err := c.ShouldBindQuery(&q); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	list, err := h.Srv.List(&q)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	total, err := h.Srv.GetTotal()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	totalPage := 0
	pageSize := q.PageSize
	if pageSize == 0 {
		pageSize = 5
	}
	if total != 0 {
		ret := int(total % pageSize)
		ret2 := int(total / pageSize)
		if ret == 0 {
			totalPage = ret2
		} else {
			totalPage = ret2 + 1
		}
	}

	var newList []*resp.Order
	for _, item := range list {
		reqOrder := h.GetEntity(*item)
		newList = append(newList, &reqOrder)
	}

	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      list,
		Total:     total,
		TotalPage: totalPage,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *OrderHandler) AddOrderHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var order model.Order
	if err := c.ShouldBindJSON(&order); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	result := h.Srv.Add(order)
	r := h.GetEntity(*result)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		Total:     0,
		TotalPage: 0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *OrderHandler) EditOrderHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var order model.Order
	if err := c.ShouldBindJSON(&order); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	b, err := h.Srv.Edit(order)
	if err != nil {
		entity.Msg = err.Error()
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *OrderHandler) DeleteOrderHanlder(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	id := c.Param("id")
	b, err := h.Srv.Delete(id)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}
